import Vue from "vue";
import VueRouter from "vue-router";
import login from "../components/login.vue";
import home from "@/components/home";
import welcome from "@/components/welcome";
import user from "@/components/user/user";
import order from '@/components/order/order';
import admin from '@/components/user/admin';
import usertype from '@/components/user/usertype';
import entrance from '@/components/entrance';

Vue.use(VueRouter);

const routes = [];

const router = new VueRouter({
  //mode: 'history',
  routes: [
    { path: "/", redirect: "/login" },
    { path: "/login", component: login },
    { path: "/entrance", component: entrance },
    {
      path: "/home",
      component: home,
      redirect: "/welcome",
      // 子路由
      children: [
        { path: "/welcome", component: welcome },
        { path: "/user", component: user },
        { path: "/orders", component: order },
        { path: "/admin", component: admin },
        { path: "/usertype", component: usertype },
      ]
    }
  ]
});

//挂载路由导航守卫
router.beforeEach((to, from, next) => {
  //to 将要访问的路径
  //from 代表从哪个路径跳转而来
  //next 是一个函数，表示放行
  //     next()放行 next('/login)强制跳转
  if (to.path === "/login") return next();
  if (to.path === "/entrance") return next();

  //获取token
  const token = window.sessionStorage.getItem("token");

  console.log(token)
  if (!token) return next("/login");
  next();

});
export default router;
